package com.tesis_2.DA;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.tesis_2.MO.MOLocal;
import com.tesis_2.MO.MORestaurant;

public class DAOLocales {

	public static ArrayList<MOLocal> getLocales() throws JSONException {
		//DAORestaurants.lsRest = lsRest;
		
		ArrayList<MOLocal> lsLocales = new ArrayList<MOLocal>();
		
	    try{

	    	JSONArray locales = (new JSONReaderArray().execute(DAOUtils.getServer()+ "/locals.json")).get();
	    	
	    	if (locales == null) return lsLocales;
	       	    	
	    	for (int i = 0; i < locales.length(); i++) {
	    		JSONObject jsonlocales = locales.getJSONObject(i);
	    	    jsonlocales = jsonlocales.getJSONObject("local");
	    	    //obtengo el restaurante
	    	    MOLocal objLocal = new MOLocal();
	    	    //entro al objeto y leo los atributos
	    	    objLocal.setName(jsonlocales.getString("name"));
	    	    objLocal.setId(jsonlocales.getInt("id")) ;
	    	    objLocal.setDescr(jsonlocales.getString("descr"));
	    	    objLocal.setAddr1(jsonlocales.getString("addr1"));
	    	    objLocal.setAddr2(jsonlocales.getString("addrs2"));
	    	    objLocal.setPhone1(jsonlocales.getString("phone1"));
	    	    objLocal.setPhone2(jsonlocales.getString("phone2"));
	    	    objLocal.setCapacity(jsonlocales.getInt("capacity"));
	    	    objLocal.setRest_id(jsonlocales.getInt("restaurant_id"));
	    	    objLocal.setStatus(jsonlocales.getInt("status"));
	    	    objLocal.setRate((float) (jsonlocales.getDouble("rate")));
	    	    objLocal.setUpdated_at(DAOUtils.parseDateIso(jsonlocales.getString("updated_at")));
	    	    objLocal.setCreated_at(DAOUtils.parseDateIso(jsonlocales.getString("created_at")));
	    	    
	    	  //objLocal.setPhoto(jsonlocales.getString("photo_file_name"));
	    	    //{"local":{"addr1":"Address1","addrs2":"Address2","capacity":20,"created_at":"2012-12-10T02:48:59Z",
	    	    //"descr":"Centro de Lima","id":1,"name":"Centro","phone1":"123465","phone2":"78946",
	    	    //"restaurant_id":1,"status":1,"updated_at":"2012-12-10T02:49:22Z"}}
	    	    
	    	    lsLocales.add(objLocal);
	    	}
	 
	    }catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return lsLocales;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return lsLocales;
		}
		return lsLocales;
		
	}
	
	public static MOLocal createLocal(MOLocal objLocal) throws JSONException {
		//DAORestaurants.lsRest = lsRest;
		
		MOLocal Local = new MOLocal();
		
	    try{
	    	/*
	    	 * http://192.168.1.107:3000/locals/createLocal/hola/mundo/add/add2/1234/4567/10/1.json
	    	*/
	    	String url = DAOUtils.getServer()+"/locals/createLocal" 
	    									+ "/" + objLocal.getName() +
	    									"/" + objLocal.getDescr() +
	    									"/" + objLocal.getAddr1() +
	    									"/" + objLocal.getAddr2() +
	    									"/" + objLocal.getPhone1() +
	    									"/" + objLocal.getPhone2() +
	    									"/" + objLocal.getCapacity() +
	    									"/" + objLocal.getRest_id() +
	    									".json";
	    	
	    	JSONObject jsonlocal = (new JSONReaderObject().execute(url)).get();
	    	
	    	if (jsonlocal == null) return Local;
	       	    	
	    	
	    	JSONObject jsonLocales = jsonlocal.getJSONObject("dscore");
	    	   
	    	/*
	    	 * {"local":{"addr1":"add","addrs2":"add2",
	    	 * "capacity":10,"created_at":"2013-02-11T01:52:08Z"
	    	 * ,"descr":"mundo","id":13,"name":"hola","phone1":
	    	 * "1234","phone2":"4567","rate":3.0,"restaurant_id":1,
	    	 * "status":1,"updated_at":"2013-02-11T01:52:08Z"}}
	    	 * 
	    	 * 
	    	 */
	    	 Local.setName(jsonLocales.getString("name"));
	    	 Local.setAddr1(jsonLocales.getString("addr1"));
	    	 Local.setAddr2(jsonLocales.getString("addrs2"));
	    	 Local.setCapacity(jsonLocales.getInt("capacity"));
	    	 Local.setDescr(jsonLocales.getString("descr"));
	    	 Local.setId(jsonLocales.getInt("id"));
	    	 Local.setPhone1(jsonLocales.getString("phone1"));
	    	 Local.setPhone2(jsonLocales.getString("phone2"));
	    	 Local.setRate(((float)(jsonLocales.getDouble("rate"))));
	    	 Local.setRest_id(jsonLocales.getInt("restaurant_id"));
	    	 
	   	    //[{"commentary":{"cmt":"Esto es un comentario de prueba.","created_at":"2012-12-11T22:52:43Z",
	   	    //"id":1,"local_id":2,"rate":3,"reservation_id":1,"restaurant_id":2,
    	    //"updated_at":"2012-12-11T22:52:43Z","user_id":1,"usrname":"Hunter"}}]
	    	    
	    	
	 
	    }catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Local;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Local;
		}
		
		return Local;
		
	}
	
}
